package com.zhangc.blog.admin.web.controller.admin;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhangc.blog.admin.entity.BlogConfigEntity;
import com.zhangc.blog.admin.service.BlogConfigServiceImpl;
import com.zhangc.blog.admin.web.dto.AjaxResultPage;
import com.zhangc.blog.base.ResultBO;

/**
 * @qq交流群 796794009
 * @qq 1320291471
 * @Description: blog配置controller
 * @create 2020/12/27
 */
@Controller
@RequestMapping("/admin")
public class ConfigController {

    @Autowired
    private BlogConfigServiceImpl blogConfigService;

    /**
     * 跳转系统配置界面
     *
     * @return java.lang.String
     * @date 2019/8/29 19:11
     */
    @GetMapping("/v1/blogConfig")
    public String gotoBlogConfig() {
        return "adminLayui/sys-edit";
    }

    /**
     * 返回系统配置信息
     *
     * @param
     * @return com.site.blog.pojo.dto.AjaxResultPage<com.site.blog.entity.BlogConfig>
     * @date 2019/8/29 19:30
     */
    @ResponseBody
    @GetMapping("/v1/blogConfig/list")
    public AjaxResultPage<BlogConfigEntity> getBlogConfig() {
        AjaxResultPage<BlogConfigEntity> ajaxResultPage = new AjaxResultPage<>();
        List<BlogConfigEntity> list = blogConfigService.list();
        if (CollectionUtils.isEmpty(list)) {
            ajaxResultPage.setCode(500);
            return ajaxResultPage;
        }
        ajaxResultPage.setData(blogConfigService.list());
        return ajaxResultPage;
    }

    /**
     * 修改系统信息
     *
     * @param blogConfig
     * @return com.site.blog.pojo.dto.Result
     * @date 2019/8/29 19:45
     */
    @ResponseBody
    @PostMapping("/v1/blogConfig/edit")
    public ResultBO<String> updateBlogConfig(BlogConfigEntity blogConfig) {
        blogConfig.setUpdateTime(new Date());
        boolean flag = blogConfigService.updateById(blogConfig);
        if (flag) {
            return ResultBO.success();
        } else {
            return ResultBO.error500(null);
        }
    }

    @GetMapping("/v1/blogConfig/add")
    public String addBlogConfig() {
        return "adminLayui/sys-add";
    }

    /**
     * 新增系统信息项
     *
     * @param blogConfig
     * @return com.site.blog.pojo.dto.Result
     * @date 2019/8/30 10:57
     */
    @ResponseBody
    @PostMapping("/v1/blogConfig/add")
    public ResultBO<String> addBlogConfig(BlogConfigEntity blogConfig) {
        blogConfig.setCreateTime(new Date());
        blogConfig.setUpdateTime(new Date());
        boolean flag = blogConfigService.save(blogConfig);
        if (flag) {
            return ResultBO.success();
        } else {
            return ResultBO.error500(null);
        }
    }

    /**
     * 删除配置信息项
     *
     * @param configField
     * @return com.site.blog.pojo.dto.Result
     * @date 2019/8/30 11:21
     */
    @ResponseBody
    @PostMapping("/v1/blogConfig/del")
    public ResultBO<String> delBlogConfig(@RequestParam String configField) {
        boolean flag = blogConfigService.removeById(configField);
        if (flag) {
            return ResultBO.success();
        } else {
            return ResultBO.error500(null);
        }
    }
}
